package com.xuhuan.demo.file;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;

import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author huan.xu
 * @Date 2023/8/2 17:34
 * @Description 比较工具类
 */
@Slf4j
public class CompareUtil {

    private CompareUtil() {

    }

    public static void main(String[] args) {
        String beforeDir = "C:\\Users\\huan.xu\\Desktop\\jar升级\\升级前";
        String afterDir = "C:\\Users\\huan.xu\\Desktop\\jar升级\\升级后";

        Set<String> beforeSet = new HashSet<>();
        Set<String> afterSet = new HashSet<>();

        FileUtil.getDirFileSet(new File(beforeDir), beforeSet);
        FileUtil.getDirFileSet(new File(afterDir), afterSet);

        if (CollectionUtils.isNotEmpty(beforeSet) && CollectionUtils.isNotEmpty(afterSet)) {
            // 获取删除的文件
            log.info("----------------删除的文件如下----------------");
            List<String> delList = beforeSet.stream()
                    .filter(s -> !afterSet.contains(s))
                    .collect(Collectors.toList());
            delList.stream().forEach(s -> log.info(s));

            // 获取新增的文件
            log.info("----------------新增的文件如下----------------");
            List<String> addList = afterSet.stream()
                    .filter(s -> !beforeSet.contains(s))
                    .collect(Collectors.toList());
            addList.stream().forEach(s -> log.info(s));
        }


    }
}
